package basic;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class Paixu {

	/**
	 * @param args
	 * shuffle阶段是有一个默认排序的，
	 */
	public static void main(String[] args) throws Exception{
		if(args.length!=2){
			System.err.println("user inpath err !");
			System.exit(-1);
		}
		Job job=new Job(new Configuration(),"Paixu");
		job.setJarByClass(Paixu.class);
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job,new Path(args[1]));
		
		job.setMapperClass(pmap.class);
		job.setReducerClass(preduce.class);
		
		job.setMapOutputKeyClass(IntWritable.class);
		job.setMapOutputValueClass(NullWritable.class);
		
		job.setOutputKeyClass(IntWritable.class);
		job.setOutputValueClass(IntWritable.class);
		job.waitForCompletion(true);
		
		
	}
	public static class pmap extends Mapper<LongWritable,Text,IntWritable,NullWritable>{
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,IntWritable,NullWritable>.Context context) throws java.io.IOException ,InterruptedException {
			String line = value.toString();
			int par = Integer.parseInt(line);
			
			context.write(new IntWritable(par),NullWritable.get());
		};
	}
	//map的中间结果格式《544，null》
	//中间结果需要经过shuffle处理，可以通过其默认的排序功能对key进行排序，（默认情况）如果是数字则按升序排列。从小到大，如果是文本，就按照字典顺序进行排序。
	
	public static class preduce extends Reducer<IntWritable,NullWritable,IntWritable,IntWritable>{
		int sortnum=0;
		protected void reduce(IntWritable k2, java.lang.Iterable<NullWritable> values, org.apache.hadoop.mapreduce.Reducer<IntWritable,NullWritable,IntWritable,IntWritable>.Context context) throws java.io.IOException ,InterruptedException {
		sortnum++;
		context.write(new IntWritable(sortnum), k2);
		};
	}

}
